﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;

using System.ServiceModel.DomainServices.Client;

using LEEAAttendanceControl.AttendanceReference;
using System.ComponentModel;
using System.ServiceModel;

using System.Collections.ObjectModel;

using LEEAAttendanceControl.Web;
using LEEAAttendanceControl.Web.Services;

using System.Windows.Browser;

namespace LEEAAttendanceControl.Views
{
    public partial class Reports : Page
    {
        LEEAContext context = new LEEAContext();

        private CheckBox checkAllBox = null;

        ObservableCollection<int> employees = new ObservableCollection<int>();

        public Reports()
        {
            InitializeComponent();

            ChangeEmployees();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            App myApp = (App)Application.Current;

            myApp.ClearEvents();

            myApp.ReportName = "Summary Time Card Report";

            myApp.ChangeEmployeesEvent += ChangeEmployeesHandler;
            myApp.ChangeReportTitleEvent += ChangeTitleHandler;

            ChangeEmployees();
        }

        private void ChangeTitleHandler(object sender, EventArgs e)
        {
            App myApp = (App) Application.Current;

            ReportNameBlock.Text = myApp.ReportName;

            //================================================================
            //--> When User change Report Type, clear all selection:

            employees.Clear();

            if (checkAllBox != null && checkAllBox.IsChecked.Value)
            {
                checkAllBox.IsChecked = false;
            }

            foreach (var item in EmployeesGrid.ItemsSource)
            {
                EmployeeSummary employee = item as EmployeeSummary;

                if (employee.IsChecked)
                {
                    employee.IsChecked = false;
                }
            }

            FromPicker.SelectedDate = null;
            ToPicker.SelectedDate = null;
        }

        public void ChangeEmployeesHandler(object sender, EventArgs e)
        {
            ChangeEmployees();
        }

        void ChangeEmployees()
        {
            if (checkAllBox != null && checkAllBox.IsChecked.Value)
            {
                checkAllBox.IsChecked = false;
            }

            EmployeesGrid.ItemsSource = null;

            context.EmployeeSummaries.Clear();

            int companyID = ((App)Application.Current).CompanyID ?? 0;

            int departmentID = ((App)Application.Current).DepartmentID ?? 0;

            int employeeID = ((App)Application.Current).EmployeeId;

            context.Load(context.GetFilteredEmployeeSummariesQuery(companyID, departmentID, employeeID)
                , OnEmployeesLoadCompleted, null);
        }

        private void OnEmployeesLoadCompleted(LoadOperation load)
        {
            EmployeesGrid.ItemsSource = load.Entities;
        }

        private void SelectAllCheckBox_Click(object sender, RoutedEventArgs e)
        {
            employees.Clear();

            CheckBox selector = sender as CheckBox;

            if (checkAllBox == null)
            {
                checkAllBox = selector;
            }

            foreach (var item in EmployeesGrid.ItemsSource)
            {
                EmployeeSummary emp = item as EmployeeSummary;

                emp.IsChecked = selector.IsChecked.Value;

                if (selector.IsChecked.Value)
                {
                    employees.Add(emp.EmployeeID);
                }
            }
        }

        private void EmployeeCheck_Click(object sender, RoutedEventArgs e)
        {
            EmployeeSummary emp = EmployeesGrid.SelectedItem as EmployeeSummary;

            if (emp.IsChecked)
            {
                employees.Add(emp.EmployeeID);
            }
            else
            {
                employees.Remove(emp.EmployeeID);

                if (checkAllBox != null && checkAllBox.IsChecked.Value)
                {
                    checkAllBox.IsChecked = false;
                }
            }
        }

        private void GenerateButton_Click(object sender, RoutedEventArgs e)
        {
            AttendanceServiceClient proxy = General.General.GetProxy();

            proxy.SetReportDataCompleted += OnDataSettingCompleted;

            int companyID = ((App)Application.Current).CompanyID == null
                ? 0 : ((App)Application.Current).CompanyID.Value;

            proxy.SetReportDataAsync(employees, FromPicker.SelectedDate.Value.Date, ToPicker.SelectedDate.Value.Date
                , companyID);
        }

        private void OnDataSettingCompleted(object sender, AsyncCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                string report = String.Empty;

                switch (((App)Application.Current).ReportName)
                {
                    case "Summary Time Card Report":
                        report = "SummaryTimeCard";

                        break;

                    case "Detailed Time Card Report":
                        report = "DetailedTimeCard";

                        break;

                    case "Detailed Overtime Report":
                        report = "DetailedOvertime";

                        break;

                    case "Shift Assignments Report":
                        report = "ShiftAssignments";

                        break;

                    case "Exception Assignments Report":
                        report = "ExceptionAssignments";

                        break;

                    case "OnSite Presence Report":
                        report = "OnSitePresence";

                        break;

                    case "Late Comers Report":
                        report = "LateComers";

                        break;

                    case "Absence Report":
                        report = "Absence";

                        break;

                    case "Saved Hours Report":
                        report = "SavedHours";

                        break;

                    case "Missing Punches Report":
                        report = "MissingPunches";

                        break;
                }

                string url = General.General.GetUrl()+ "/ReportPages/Reports.aspx?Name=" + report;

                HtmlPage.Window.Navigate(new Uri(url), "_newWindow"
                    , "resizable=1,scrollbars=1,status=1,top=110,left=75,width=1130,height=606");

                //--> 'HtmlPage.Window.Invoke()' method can be used to call JavaScript Code:
                //    HtmlPage.Window.Invoke("ShowReportWindow", url);
            }
            else
            {
                MessageDisplay message = new MessageDisplay(ApplicationStrings.GeneralError
                    , ApplicationStrings.GeneralError, "Error");

                message.Show();
            }
        }
    }
}
